import store from '../../westore/store'
import create from '../../westore/utils/create'

import Config from "../../config"
import Api from "../../utils/api"
import {
  Modal
} from "../../utils/common"
import deepCopy from "../../utils/common"
const app = getApp(),
  regeneratorRuntime = require("../../utils/regenerator-runtime/runtime")

create(store, {
  data: {
    openid: "",
    userInfo: null,
    gender:["保密","男","女"]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  nameInput: function (e) {
    const _this = this;
    _this.store.data.userInfo.nickName = e.detail.value;
    _this.update();
  },
  cityInput: function (e) {
    const _this = this;
    _this.store.data.userInfo.city = e.detail.value;
    _this.update();
  },
  contactInput: function (e) {
    const _this = this;
    _this.store.data.userInfo.contact = e.detail.value;
    _this.update();
  },
  genderChange: function (e) {
    const _this = this;
    console.log(e.detail)
    _this.store.data.userInfo.gender = Number(e.detail.value);
    _this.update();
  },
  // 点击选择图片
  selectImg: function () {
    const _this = this
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) { // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        // 数据显示
        const tempFilePaths = res.tempFilePaths[0]
        _this.uploadImg(tempFilePaths);
      }
    })
  },

  //上传图片
  uploadImg(filePaths) {
    const _this = this
    const openid = _this.store.data.openid;
    const up = wx.uploadFile({
      url: `${Config.apiUrl}image/upload`,
      filePath: filePaths,
      name: 'image',
      header: {
        "content-type": "multipart/form-data"
      },
      formData: {
        openid: openid,
      },
      success: (res) => {
        console.log(res);
        _this.store.data.userInfo.avatarUrl = Config.baseUrl + JSON.parse(res.data).data.image
        _this.update();
        _this.setData({
          avatarData: res.data
        })
      },
      fail: (res) => {
        const data = JSON.parse(res.data)
      },
    });
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})